package com.PVZ.view;

import com.PVZ.engine.Stage;
import com.PVZ.engine.map.MapBlock;
import com.PVZ.model.cards.PlantCard;
import com.PVZ.model.emuns.PlantType;
import com.PVZ.utils.TimeTaskPool;
import lombok.Getter;
import lombok.Setter;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
 * Created by @Author: kabuda on @Date: 2021/5/24 22:10
 * <p>
 * 开始时选择卡片
 */
@Getter
@Setter
public class CardView extends JFrame {

    public static final int START_X = 220;
    private Graphics graphics;
    public static ArrayList<PlantCard> plantCards = new ArrayList<>();
    public static ArrayList<PlantCard> upCards = new ArrayList<>();
    private Stage stage;

    public CardView(Graphics graphics, Stage stage) {
        this.graphics = graphics;
        this.stage = stage;

        PlantCard sunflower = new PlantCard(PlantType.SunFlower, 0);
        PlantCard peaShooterSingle = new PlantCard(PlantType.PeaShooterSingle, 1);
        PlantCard wallNut = new PlantCard(PlantType.WallNut, 2);
        PlantCard peaShooter = new PlantCard(PlantType.PeaShooter, 3);
        // PlantCard caltrop = new PlantCard(PlantType.Caltrop, 4);
        PlantCard cherryBomb = new PlantCard(PlantType.CherryBomb, 5);
        PlantCard tallbut = new PlantCard(PlantType.Tallnut, 6);
        // PlantCard potatoMine = new PlantCard(PlantType.PotatoMine, 7);

        //太阳花
        plantCards.add(sunflower);
        upCards.add(sunflower);
        // 豌豆单射手
        plantCards.add(peaShooterSingle);
        upCards.add(peaShooterSingle);
        //豌豆双射手
        plantCards.add(peaShooter);
        upCards.add(peaShooter);
        //小坚果
        plantCards.add(wallNut);
        upCards.add(wallNut);
        // //地刺
        // plantCards.add(caltrop);
        // upCards.add(caltrop);
        //樱桃炸弹
        plantCards.add(cherryBomb);
        upCards.add(cherryBomb);

        //土豆雷
        // plantCards.add(tallbut);
        // upCards.add(tallbut);

        //设置卡片位置
        setCardsLocation(plantCards);


    }

    public static void drawCard(Graphics graphics) {
        int i = 0;
        for (PlantCard upCard : upCards) {
            if(Stage.sunQuantity>=plantCards.get(i).getValue()){
                graphics.drawImage(upCard.getCurImage(), START_X + i * PlantCard.WIDTH, 7, null);
            }else{
                graphics.drawImage(upCard.getUCurImage(), START_X + i * PlantCard.WIDTH, 7, null);
            }

            i++;
        }

        //绘制阳光数量
        graphics.drawString(String.valueOf(Stage.sunQuantity),170,80);
    }

    public void repaintToolBar(){
        drawCard(graphics);
        drawToolBar();
    }

    /**
     * 种植物
     *
     * @param card  被种的卡
     * @param block 种植的单元格
     */
    public void plantCard(PlantCard card, MapBlock block) {
        //plantFactory生成植物
        card.setCurImage(card.getGrayImage());
        drawCard(graphics);
        stage.getPlantFactory().generatePlant(card.getPlantType(), block.getRow(), block.getColumn());
        block.setHasPlant(true);

        //改变植物卡片状态
        card.setClickStatus(false);
        //定时一秒
        if (card.getCurTime() >= card.getCoolDown()) {
            card.setCurTime(0.0);
        } else {
            TimeTaskPool.timerPool.scheduleAtFixedRate(() -> {
                if (card.getCurTime() >= card.getCoolDown()) {
                    card.setCurTime(0.0);
                    card.setCurImage(card.getLightImage());
                    card.setClickStatus(true);
                    drawCard(graphics);
                    try {
                        Thread.sleep(-1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                } else {
                    BufferedImage image = grayImageToLight(card.getLightImage(), card.getGrayImage(), card.getCoolDown(), card.getCurTime());
                    card.setCurImage(image);
                    drawCard(graphics);
                    card.setCurTime(card.getCurTime() + 1);
                }
                //延迟0秒启动 每1s启动一次
            }, 0, 1, TimeUnit.SECONDS);
        }

        //重新设置卡片位置
        setCardsLocation(plantCards);

        setCardsLocation(upCards);
    }

    public void setCardsLocation(ArrayList<PlantCard> cards) {
        for (int i = 0; i < cards.size(); i++) {
            if (i < 7) {
                cards.get(i).setLocation(i * WIDTH, 210);
            } else {
                cards.get(i).setLocation(i * WIDTH - 350, 290);
            }
        }
    }


    public void drawToolBar() {
        try {
            BufferedImage cardplace = ImageIO.read(new File("res/img/card/seedBank.png"));
            BufferedImage shovel = ImageIO.read(new File("res/img/card/shovel.png"));
            Image shovelBank = new ImageIcon("res/img/card/shovelBank.png").getImage();

            graphics.drawImage(cardplace,140,0,null);
            graphics.drawImage(shovelBank,cardplace.getWidth()+140,0,null);
            graphics.drawImage(shovel,cardplace.getWidth()+130,0,null);

            graphics.setFont(new Font("Arial", Font.BOLD,20));
            graphics.setColor(Color.WHITE);
            //视关卡改变
            CardView.drawCard(graphics);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
     * 将灰色图片从下往上变亮
     *
     * @param lightImage 亮色图片
     * @param grayImage  灰色图片
     * @param coolDown   冷却时间
     * @param curTime    当前冷却时间
     * @return BufferedImage
     */
    public BufferedImage grayImageToLight(BufferedImage lightImage, BufferedImage grayImage, Integer coolDown, Double curTime) {
        //分割线高度
        int cut = 72 - (int) (72 * curTime / coolDown);
        int height = lightImage.getHeight();
        int width = lightImage.getWidth();
        //从图片中读取RGB
        int[] grayImageArray = new int[height * width];
        int[] lightImageArray = new int[height * width];

        //逐行扫描图像中的各个像素的RGB到数组中
        grayImage.getRGB(0, 0, width, cut, grayImageArray, 0, width);
        lightImage.getRGB(0, cut, width, height - cut, lightImageArray, 0, width);

        //垂直方向合并
        BufferedImage destImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        destImage.setRGB(0, 0, width, cut, grayImageArray, 0, width);
        destImage.setRGB(0, cut, width, height - cut, lightImageArray, 0, width);


        return destImage;
    }
}
